#!/bin/bash
<<'commenct' 
echo -en "\n欢迎使用一键修改插件\n"
echo -en "\n  by          梦殇  \n"
echo -en "\n  by   酷安:  ベ浮生若梦ミ  \n"
echo -en "\n  by   QQ:   858145049  \n"
commenct
			
<<'commenct' 
if [[ $(uname -m) == "aarch64" ]]; then
	su=" "
	export PATH=$PATH:$SHELL_DIR/Insides/Droid
else
	su="sudo "
	export PATH=$PATH:$SHELL_DIR/Insides/Linux
fi

export USER=$(echo "$(whoami | gawk '{ print $1 }')")
commenct

SHELL_DIR="$(cd $(dirname $0); pwd)"
PROJECT_DIR=$1
SYSTEM_DIR=$2
system_folder_name=`ls *system* -d`
if [[ ! -d ${SYSTEM_DIR} ]]; then
	echo "无SYSTEM目录，爪巴！"
	exit 1
fi
echo "Project folder:$PROJECT_DIR" 
echo "Sub tool: $SHELL_DIR"
echo "System dir:$SYSTEM_DIR"

if [[ -f $SHELL_DIR/Insides/apktool/apktool.jar ]]; then
	ApkTool="java -jar $SHELL_DIR/Insides/apktool/apktool.jar" 
	echo "ApkTool version:" && $ApkTool -version
	
else
	echo "Not found ApkTool"
	exit 1
fi

if [ -d $SHELL_DIR/services ];then
	echo "發現舊的...刪除"
	${su}rm -rf $SHELL_DIR/services 2>/dev/null
fi

taichi_install(){
echo -en "\n嘗試太極陽添加\n"
	if [[ -f $SYSTEM_DIR/bin/app_process32 && \
	      -f $SYSTEM_DIR/bin/app_process64 && \
	      -d $SYSTEM_DIR/data-app/taichi && \
	      -d $SYSTEM_DIR/app/taichi && \
	      -f $SYSTEM_DIR/framework/taichi_magisk60.jar && \
	      -f $SYSTEM_DIR/lib/libjit.so && \
	      -f $SYSTEM_DIR/lib64/libjit.so ]];then
		cp -rf $SHELL_DIR/Insides/taichi/* $SYSTEM_DIR/
	else
		echo "found taichi app data"
		echo "maybe check..."
		echo "$SYSTEM_DIR/bin/app_process32"
		echo "$SYSTEM_DIR/bin/app_process64"
		echo "$SYSTEM_DIR/data-app/taichi"
		echo "$SYSTEM_DIR/framework/taichi_magisk60.jar"
		echo "$SYSTEM_DIR/lib/libjit.so"
		echo "$SYSTEM_DIR/lib64/libjit.so"
	fi
	
	cp -rf $SHELL_DIR/Insides/taichi/data-app/taichi $SYSTEM_DIR/
	cp -rf $SYSTEM_DIR/framework/taichi_magisk60.jar $SYSTEM_DIR/
	cp -rf $SYSTEM_DIR/lib/libjit.so $SYSTEM_DIR/
	cp -rf $SYSTEM_DIR/lib64/libjit.so $SYSTEM_DIR/
	
}

#taichi_install()


echo -en "\n嘗試電源功能強化\n"
if [ -d $SYSTEM_DIR/media/theme/default/powermenu ]; then
	Crack_File1=$(find $SHELL_DIR/services/ -type f -name '*.smali' 2>/dev/null | xargs grep -rl '.method public onCommand(Ljava\/lang\/String;Ljava\/lang\/Double;Ljava\/lang\/String;)V' | sed 's/^\.\///' | sort)
	sed -i '/^.method public onCommand(Ljava\/lang\/String;Ljava\/lang\/Double;Ljava\/lang\/String;)V/,$d' $Crack_File1
	cat $SHELL_DIR/Insides/power/powermenu.txt >> $Crack_File1
	cp -rf $SHELL_DIR/Insides/power/powermenu.zip $SYSTEM_DIR/media/theme/default/powermenu
	echo -e "\e[32msuccess\e[0m"
else
	echo "Not found dirctory, skip"
fi


if [[ -f $SYSTEM_DIR/framework/services.jar ]]; then
	echo -e "\n嘗試小米相關功能"
	if [[ $(grep "classes.dex" $SYSTEM_DIR/framework/services.jar) != "" ]]; then	
		if [[ $(find $SYSTEM_DIR/ -name miui.apk 2>/dev/null) && $(find $SYSTEM_DIR/ -name miuisystem.apk) ]]; then
			$ApkTool d -q -r -f -o $SHELL_DIR/services $SYSTEM_DIR/framework/services.jar
			$ApkTool d -q -r -f -o $SHELL_DIR/ThemeManager $SYSTEM_DIR/app/ThemeManager/ThemeManager.apk

			if [ -d $SHELL_DIR/services ]; then
				echo -en "嘗試去除卡米"
				Crack_File=$(find $SHELL_DIR/services/ -type f -name '*.smali' 2>/dev/null | xargs grep -rl '.method private checkSystemSelfProtection(Z)V' | sed 's/^\.\///' | sort)
				sed -i '/^.method private checkSystemSelfProtection(Z)V/,/^.end method/{//!d}' $Crack_File
				sed -i -e '/^.method private checkSystemSelfProtection(Z)V/a\    .locals 1\n\n    return-void' $Crack_File

				if [ -d $SHELL_DIR/ThemeManager ]; then
					echo -en "嘗試MIUI 主题破解"
					Crack_File2=$(find $SHELL_DIR/ThemeManager/ -type f -name '*.smali' 2>/dev/null | xargs grep -rl 'DRM_ERROR_UNKNOWN' | sed 's/^\.\///' | sort)
					sed -i 's/DRM_ERROR_UNKNOWN/DRM_SUCCESS/g' $Crack_File2
					Crack_File3=$(find $SHELL_DIR/ThemeManager/ -type f -name '*.smali' 2>/dev/null | xargs grep -rl 'OnlineResourceDetail' | grep 'OnlineResourceDetailPresenter' | sed 's/^\.\///' | sort)
					sed -i '/OnlineResourceDetail;->bought:Z/i\const/4 v0, 0x1' $Crack_File3
					sed -i '/OnlineResourceDetail;->bought:Z/i\ return v0' $Crack_File3
				    Crack_File4=$(find $SHELL_DIR/ThemeManager/ -type f -name '*.smali' 2>/dev/null | xargs grep -rl 'AdInfo;->targetType:I' | grep -rl 'AdInfo' | sed 's/^\.\///' | sort)
				    sed -i '/AdInfo;->targetType:I/i\const/4 v0, 0x0' $Crack_File4
				    sed -i '/AdInfo;->targetType:I/i\ return v0' $Crack_File4
				    Crack_File5=$(find $SHELL_DIR/ThemeManager/ -type f -name '*.smali' 2>/dev/null | xargs grep -rl 'Parcel;->obtain()Landroid/os/Parcel;' | grep -rl 'IAdListener$Stub$Proxy' | sed 's/^\.\///' | sort)
				    sed -i '/Parcel;->obtain()Landroid\/os\/Parcel;/i\ return-void' $Crack_File5
					Crack_File6=$(find $SHELL_DIR/ThemeManager/ -type f -name '*.smali' 2>/dev/null | xargs grep -rl '"using_theme_show_ad"' | grep -rl 'const-string v0, "using_theme_show_ad"' | sed 's/^\.\///' | sort)
				    sed -i '/"using_theme_show_ad"/i\const/4 v0, 0x0'  $Crack_File6
				    sed -i '/"using_theme_show_ad"/i\ return v0'  $Crack_File6
					echo -e "\e[32msuccess\e[0m"
				fi
				
				echo -en "嘗試MIUI-build.prop文件版本 更改"
				build=$(find $PROJECT_DIR -name "build.prop")
				for file1 in $build; do
					sed -i "s/ro.build.version.incremental=.*/ro.build.version.incremental=V2000.12.01-15.SGHCNXM/g" $file1
					sed -i "s/ro.system.build.version.incremental=.*/ro.system.build.version.incremental=V2000.12.01-15.SGHCNXM/g" $file1
					sed -i "s/ro.vendor.build.version.incremental=.*/ro.vendor.build.version.incremental=V2000.12.01-15.SGHCNXM/g" $file1
					sed -i "s/ro.odm.build.version.incremental=.*/ro.odm.build.version.incremental=V2000.12.01-15.SGHCNXM/g" $file1
					sed -i "s/ro.product.build.version.incremental=.*/ro.product.build.version.incremental=V2000.12.01-15.SGHCNXM/g" $file1
				done
				
				
				echo -en "Apktool 编译\n"
				$ApkTool b -q -f -o $SYSTEM_DIR/framework/services.jar $SHELL_DIR/services
				$ApkTool b -q -f -o $SYSTEM_DIR/app/ThemeManager/ThemeManager.apk $SHELL_DIR/ThemeManager
				${su}rm -rf $SHELL_DIR/services 2>/dev/null
				echo -e "\e[32msuccess\e[0m"				
			fi

		fi
	else
		echo "Not found miui.apk and miuisystem.apk, skip"

	fi
fi			

echo -en "\n嘗試avb 檢查和data 加密去除\n" #/vendor
fstab=$(find $PROJECT_DIR -name "fstab*")
if [[ $recovery == "" ]];then
	echo "Not found file, skip"
else
	for file in $fstab; do
		sed -i 's/,avb.*m,/,/g' $file
		sed -i 's/,avb,/,/g' $file
		sed -i "s/,avb.*key//g" $file
		sed -i "s/fileencryption=ice/encryptable=footer/g" $file
		echo -e "\e[32msuccess\e[0m"
	done
fi
echo -en "\n嘗試防止3rd REC開入系統後還原\n" #/vendor
recovery=$(find $PROJECT_DIR -name "recovery-from-boot.p")
if [[ $recovery == "" ]];then
	echo "Not found file, skip"
else
	for file2 in $recovery; do
		#rm -rf $file2
		mv $file2 $PROJECT_DIR
		echo -e "\e[32msuccess\e[0m"
	done
fi

echo -en "\n嘗試增加小功能，部分系統支持\n"
device_features=$(find $PROJECT_DIR -name "device_features")
if [[ $device_features == "" ]];then
	echo "Not found file, skip"
else
	for file3 in $device_features; do
		sed -i '/<\/features>/i\    <!-- 以下为一键修改插件写入 -->' $file3/*xml
	# 游戏英雄死亡倒计时
		sed -i '/<\/features>/i\    <bool name=\"support_mi_game_countdown\">true<\/bool>' $file3/*xml
	# DC调光
		sed -i '/<\/features>/i\    <bool name=\"support_dc_backlight\">true<\/bool>' $file3/*xml
	# 呼吸灯
		sed -i '/<\/features>/i\    <bool name=\"support_led_color\">true<\/bool>' $file3/*xml
		sed -i '/<\/features>/i\    <bool name=\"support_led_light\">true<\/bool>' $file3/*xml
	# 息屏显示时点击一下显示10秒
		sed -i '/<\/features>/i\    <bool name=\"aod_support_keycode_goto_dismiss\">true<\/bool>' $file3/*xml
	# 视频工具箱
		sed -i '/<\/features>/i\    <bool name=\"is_support_video_tool_box\">true<\/bool>' $file3/*xml
	# 息屏显示
		sed -i '/<\/features>/i\    <bool name=\"support_aod\">true<\/bool>' $file3/*xml
	# 高级游戏模式
		sed -i '/<\/features>/i\    <bool name=\"support_displayfeature_gamemode\">true<\/bool>' $file3/*xml
		sed -i '/<\/features>/i\    <bool name=\"support_touchfeature_gamemode\">true<\/bool>' $file3/*xml
	# HiFi
		sed -i '/<\/features>/i\    <bool name=\"support_hifi\">true<\/bool>' $file3/*xml
	# 杜比
		sed -i '/<\/features>/i\    <bool name=\"support_dolby\">true<\/bool>' $file3/*xml
	# Ai键
		sed -i '/<\/features>/i\    <bool name=\"support_ai_task\">true<\/bool>' $file3/*xml
	# 声音助手
		sed -i '/<\/features>/i\    <bool name=\"support_sound_assist\">true<\/bool>' $file3/*xml
	# 纸质护眼代码, 提取自K30
		sed -i '/<\/features>/i\    <bool name=\"support_papermode_animation\">true<\/bool>' $file3/*xml
		sed -i '/<\/features>/i\    <bool name=\"support_smart_eyecare\">true<\/bool>' $file3/*xml
		sed -i '/<\/features>/i\    <bool name=\"support_paper_eyecare\">true<\/bool>' $file3/*xml
		sed -i '/<\/features>/i\    <float name=\"paper_eyecare_default_value\">91.0<\/float>' $file3/*xml
		sed -i '/<\/features>/i\    <integer name=\"paper_eyecare_default_texture\">13<\/integer>' $file3/*xml
		sed -i '/<\/features>/i\    <integer name=\"paper_eyecare_min_texture\">0<\/integer>' $file3/*xml
		sed -i '/<\/features>/i\    <integer name=\"paper_eyecare_max_texture\">25<\/integer>' $file3/*xml
	# 支持选择屏幕刷新率
		sed -i '/<\/features>/i\   <integer-array name=\"fpsList\">' $file3/*xml
		sed -i '/<\/features>/i\    <item>144<\/item>' $file3/*xml
		sed -i '/<\/features>/i\    <item>120<\/item>' $file3/*xml
		sed -i '/<\/features>/i\    <item>90<\/item>' $file3/*xml
		sed -i '/<\/features>/i\    <item>60<\/item>' $file3/*xml
		sed -i '/<\/features>/i\    <\/integer-array>' $file3/*xml
		sed -i '/<\/features>/i\    <!-- 以上为一键修改插件写入 -->' $file3/*xml
	done
fi

chmod -R 0777 $PROJECT_DIR
echo -e "\e[32mDone\e[0m"
